package TCP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class TestSeverSocket
{
    public static void main (String[] args)
    {
        System.out.println("服务器正在启动，等待监听...");
        
        try(//创SeverSocket对象
            ServerSocket serverSocket = new ServerSocket(8888);
            //监听8888端口，此时当前线程会被阻塞
            Socket socket = serverSocket.accept();
            //链接成功后得到和客户端相同的Socket对象，解除线程阻塞
            //通过客户端对应的Socket对象中的InputStream，获取客户端发来的信息
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())))
        {
            System.out.println(bufferedReader.readLine());
        }
        catch(IOException e)
        {
            System.out.println("服务器启动失败");
            
            e.printStackTrace();
        }
    }
}
